package org.opu.gui.component;

import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 *
 */
public class MyJTabbedPane extends JTabbedPane {

    private Status status;

    private int startNumTab;

    private MyJTabbedPane() {
        removeAll();
        setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
        addChangeListener(new ChangeListenerImpl());
        startNumTab = getTabCount();

    }

    /**
     * Create instance: LayoutPolicy - SCROLL_TAB_LAYOUT
     * @param status status
     */
    public MyJTabbedPane(Status status) {
        this();
        this.status = status;
    }

    /**
     *  Reinit tab component to all tabs
     */
    public void reinitAll() {
        for (int i = 0; i < getTabCount(); i++) {
            reinitAtt(i);
        }
    }

    /**
     *  Reinit last tab component
     */
    public void reinitLast() {
        int i = getTabCount() - 1;
        reinitAtt(i);
    }

    /**
     *  Reinit tab component at i position
     * @param i - number of reiniting tab
     */
    public void reinitAtt(int i) {
        setTabComponentAt(i, new ButtonTabComponent(this, i, status));
    }

    /**
     *  Return true if tabpane have now tabs
     * @return true if tabpane have now tabs
     */
    public boolean isNowTabls(){
        return startNumTab >= getTabCount();
    }


    /**
     *  Call status method setStatus if stateChanged
     */
    class ChangeListenerImpl implements ChangeListener {

        @Override
        public void stateChanged(ChangeEvent e) {
            if (getSelectedIndex() > -1) {
                status.setStatus("" + getTitleAt(getSelectedIndex()));
            }
        }
    }
}
